$(document).ready(function() {
    dataSource = new kendo.data.DataSource({
        transport: {
            create: {
                url: appBosch.crud.create,
                dataType: "json"
            },
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            destroy: {
                url: appBosch.crud.destroy,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    start_date: {
                        editable: true,
                        nullable: false,
                        type: 'date',
                        defaultValue: new Date(appBosch.startDate),
                        validation: {
                            required: true
                        }
                    },
                    end_date: {
                        editable: true,
                        nullable: false,
                        defaultValue: new Date(appBosch.endDate),
                        type: 'date',
                        validation: {
                            required: true
                        }
                    },
                    shift_id: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.shifts[0].value,
                        validation: {
                            required: true
                        }
                    },
                    line_id: {
                        editable: false,
                        nullable: false,
                        defaultValue: appBosch.lineId
                    }
                }
            }
        }
    });

    $("#grid").kendoGrid({
        resizable: true,
        columnMenu: true,
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        dataSource: dataSource,
        toolbar: [{name: "create", text: "Agregar Equipo"}],
        columns: [
            {
                field: "shift_id", 
                title: "Turno", 
                values: appBosch.shifts,
                width: '200px'
            },
            {
                field: "start_date", 
                title: "Inicio", 
                template: "#= kendo.toString(start_date, 'd')#",
                width: '200px'
            },
            {
                field: "end_date", 
                title: "Fin", 
                template: "#= kendo.toString(end_date, 'd')#",
                width: '200px'
            },
            {command: [
                    {text: "MH", click: showMh},
                    {text: "Integrantes", click: showUsers},
                    {
                        name: "edit",
                        text: {
                            edit: "Editar",
                            update: "Actualizar",
                            cancel: "Cancelar"
                        }
                    },
                    {
                        name: "destroy",
                        text: "Eliminar"
                    }], title: "&nbsp;"}],
        editable: {
            confirmation: "�Estas seguro que deseas eliminar este equipo de trabajo?",
            mode: "popup"
        }
    });
});

function showUsers(e)
{
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var teamId = dataItem.id;
    var parts = [appBosch.urlMembers, teamId, appBosch.lineId, appBosch.areaId, appBosch.moeId];
    var url = parts.join('/');
    $(location).attr('href', url);
}

function showMh(e)
{
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var teamId = dataItem.id;
    var parts = [appBosch.urlMh, teamId, appBosch.lineId, appBosch.areaId, appBosch.moeId];
    var url = parts.join('/');
    window.open(url, 'Matriz de multihabilidades');
}
